package com.e.takeaway.framgent;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.e.takeaway.LoginActivity;
import com.e.takeaway.MainActivity;
import com.e.takeaway.R;
import com.e.takeaway.UpdatePawActivity;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class MineFramgent extends Fragment {
    private static final String FILE_NAME = "user";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_mine, container, false);
        TextView tv_nickname = (TextView) v.findViewById(R.id.tv_sname);
        TextView tv_phone = (TextView) v.findViewById(R.id.tv_phone);
        SharedPreferences sp = getContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String phone = sp.getString("phone","");
        System.out.println("--------------------------"+phone);
        String nick = sp.getString("nickname","");
        System.out.println("--------------+++++++++++------------"+nick);
        if(nick.equals("")){
            startActivity(new Intent(v.getContext(), LoginActivity.class));
        }else{
            tv_nickname.setText(nick);
            tv_phone.setText(phone);
        }

        Button button = v.findViewById(R.id.bt_logout);
        Button button1 = v.findViewById(R.id.up_password);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = sp.edit();
                editor.clear();
                editor.commit();
                startActivity(new Intent(v.getContext(), LoginActivity.class));
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(v.getContext(), UpdatePawActivity.class));
            }
        });
        return v;
    }

}